//
//  StatusCellRetweetedView.swift
//  sinaWeibo
//
//  Created by LSY on 15/12/25.
//  Copyright © 2015年 lsy. All rights reserved.
//

import UIKit
import SnapKit

class StatusCellRetweetedView: UIView {
//   保存当前底部约束的底部约束
     var bottomConstraint : Constraint?
    
//    设置外部模型的属性
    var retweetedStatus :Status?{
    
        didSet{
            
            retweetedLable.text = "@" + (retweetedStatus?.user?.name ?? "") + "  " + ( retweetedStatus?.text ?? "")
            pictureView.imageURLs = retweetedStatus?.imageURLs
//           在此处卸载掉之前的约束
        self.bottomConstraint?.uninstall()
//            通过判断来设置约束
            
            if let urls = retweetedStatus?.imageURLs where urls.count > 0 {
                //根据是否有配图视图 重新添加约束

                    //有配图视图
                    self.snp_makeConstraints { (make) -> Void in
                        //1.添加约束
                        //2.记录底部约束
                        self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
                    }
                } else {
                    //没有配图视图
                    self.snp_makeConstraints { (make) -> Void in
                        //1.添加约束
                        //2.记录底部约束
                        self.bottomConstraint = make.bottom.equalTo(retweetedLable.snp_bottom).offset(StatusCellMarigin).constraint
                    }
            }
        }
    }
    // mark --初始化
    override init(frame:CGRect){
        
        super.init(frame:frame)
        setupUI()
//        设置测试背景色
        backgroundColor = UIColor(white: 0.95, alpha: 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //    设置UI
    func setupUI(){
//        添加视图
        addSubview(retweetedLable)
        addSubview(pictureView)
//        设置约束
        retweetedLable.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(StatusCellMarigin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
        
            make.top.equalTo(retweetedLable.snp_bottom).offset(StatusCellMarigin)
            make.left.equalTo(retweetedLable.snp_left)
            
        }
//       设置转发微博视图的约束
        self.snp_makeConstraints { (make) -> Void in
         self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
        }
    }

//    懒加载视图
    private lazy var retweetedLable:UILabel = UILabel(title: "这是转发微博", color: UIColor.grayColor(), fontSize: 14, margin: StatusCellMarigin)
//    配图集合
    private lazy var pictureView :StatusPictureView = StatusPictureView()

}
